<template>
    <div id="login">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <h2>请登录</h2>
            <el-form-item label="用户名" prop="username">
                <el-input type="username" v-model="ruleForm.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="userLogin">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import * as api from '../utils/api'
    import utils from "../utils/utils";
    export default {
        name:'login',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('用户名不能为空'));
                } else {
                    if (this.ruleForm.password !== '') {
                        this.$refs.ruleForm.validateField('password');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('密码不能为空'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    password: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            userLogin(){
                this.$refs.ruleForm.validate((valid)=>{
                    if (valid){
                        this.axios({
                            method:'post',
                            url:api.USER_LOGIN,
                            data:this.ruleForm
                        }).then((data)=>{
                            console.log(data);
                            if (data.data.code===200){
                                this.$message({
                                    message:data.data.msg,
                                    type:'success',
                                });
                                utils.setStorage('user',data.data.user);
                                this.$router.push('/home');
                            }else {
                                this.$message({
                                    message:data.data.msg,
                                    type:'info',
                                });
                                this.form={
                                    username:'',
                                    password:''
                                }
                            }
                        }).catch((err)=>{})
                    }
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style lang="less" scoped>
    form {
        min-width: 40rem;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-70%);
    .el-form-item__content{
        width: 25rem;
    }
    }
    h2{
        color: #ff8bff;
        text-align: center;
        margin-bottom: 2rem;
        font-size: 3rem;
    }
    #login{
        background-image: url("../assets/chefs-1662717_960_720.jpg");
        background-repeat: no-repeat;
        background-size: 100%;
    }
</style>